package com.dissertation.service.impl;

import com.dissertation.model.Assets;
import com.dissertation.service.IAssetsFindByUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AssetsFindByUserImpl implements IAssetsFindByUser {

    @Autowired
    MongoTemplate mongoTemplate;

    private Criteria getCommon() {
        return where( "deleted" ).is( false );
    }

    @Override
    public List<Assets> findByAuthor( String name ) {
        Query query = Query.query(
                getCommon()
                        .and( "author" ).is( name )
        );
        return mongoTemplate.find( query, Assets.class );
    }

    @Override
    public List<Assets> findByRelative( String name ) {
        Query query = Query.query(
                getCommon()
                        .and( "relative" ).elemMatch(
                        where( "$eq" ).is( name )
                )
        );
        return mongoTemplate.find( query, Assets.class );
    }

    @Override
    public List<Assets> findByParticipant( String name ) {
        Query query = Query.query(
                getCommon()
                        .and( "participantNames" ).elemMatch(
                        where( "$eq" ).is( name )
                )
        );
        return mongoTemplate.find( query, Assets.class );
    }
}
